package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.model.Country;
import org.rcloud.io.service.CountryService;
import org.rcloud.io.service.WxPushServiceQcl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
 * Created by Administrator on 2019/4/2.
 */
@RestController
@RequestMapping(value = "/Country")
public class CountryController {
    @Autowired
    private CountryService countryService;

    @RequestMapping(value = "/add",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public int add(@RequestBody String params){
        System.out.println("=======params=======:"+params);
        Country country1 = JSONObject.parseObject(params,Country.class);
        Country country2 = (Country) countryService.selectByKey(country1).getInfo();
        if (null!=country2){
            System.out.println("=======已报名，不能重复报名！=======");
            return 2;
        }else {
            System.out.println("=======未报名，开始报名....=======");
            JSONObject object = JSONObject.parseObject(params);
            object.put("id", UUID.randomUUID());
            object.put("createtime", new Date());
            object.put("flag", 1);
            Country country = JSON.parseObject(object.toString(), Country.class);
            if (Integer.valueOf(countryService.insertSelective(country).getInfo().toString()) == 1) {
                System.out.println("=======报名成功，推送模板消息！=======");
                WxPushServiceQcl wxPushServiceQcl = new WxPushServiceQcl();
                wxPushServiceQcl.pushOneUser5(country.getUserid(), country.getFramid(), country.getActivityname(), country.getName(),
                        country.getTelephone(), country.getRemark(),country.getCreatetime());
                return 1;
            } else {
                System.out.println("=======报名失败！=======");
                return 0;
            }
        }
    }

    /**
     * 查询报名统计
     * */
    @RequestMapping(value = "/selectAll", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageVo<Country> selectAll(){
        return countryService.selectAll();
    }

}
